package com.Maybe.book.controller;

import com.Maybe.book.constants.Constants;
import com.Maybe.book.dao.BookDao;
import com.Maybe.book.enums.BookStatusEnum;
import com.Maybe.book.model.*;
import com.Maybe.book.service.Bookservice;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {
    @Autowired
    private  Bookservice bookservice;

    @RequestMapping(value = "/addBook",produces = "application/json")
    public String addBook(BookInfo bookInfo){
//        log.info("添加图书,request:{}",bookInfo);
        log.info("添加图书,request:"+bookInfo);
        if(!StringUtils.hasLength(bookInfo.getBookName())
        ||!StringUtils.hasLength(bookInfo.getAuthor())
        ||bookInfo.getCount()==null
        ||bookInfo.getPrice()==null
        ||!StringUtils.hasLength(bookInfo.getPublish())
        ||bookInfo.getStatus()==null){
            log.warn("添加图书,参数不合法,request:{}"+bookInfo);
            return "参数不合法";
        }
        try{
            bookservice.addBook(bookInfo);
            return "添加图书成功";

        }catch (Exception e){
            log.error("添加图书异常,e:",e);
            return "添加图书异常";
        }
    }
    @RequestMapping("/getListByPage")
    public Result<PageResult<BookInfo>> getListByPage(PageRequest pageRequest, HttpSession session){
        log.info("获取图书列表,pageRequest:{}",pageRequest);
//        if(session.getAttribute(Constants.SESSION_USER_KEY)==null){
//            return Result.unlogin();
//        }
//        UserInfo userInfo= (UserInfo) session.getAttribute(Constants.SESSION_USER_KEY);
//        if(userInfo==null||userInfo.getId()<0||userInfo.getUserName()==""){
//            return Result.unlogin();
//        }
        PageResult<BookInfo> pageResult=bookservice.getlistBypage(pageRequest);
        return Result.success(pageResult);
    }
    @RequestMapping(value = "/queryBookById",produces = "application/json")
    public BookInfo queryBookById(Integer bookId){
        log.info("通过id查询书本"+bookId);
        if(bookId==null||bookId<=0){
            return null;
        }
        BookInfo bookInfo=bookservice.queryBookById(bookId);
        return bookInfo;
    }
    @RequestMapping(value = "/updateBook",produces = "application/json")
    public String updateBook(BookInfo bookInfo) {
        log.info("修改书籍信息,bookinfo:{}", bookInfo);
        try {
            bookservice.updateBook(bookInfo);
            return "修改书籍成功";

        } catch (Exception e){
            log.error("修改书籍失败");
            return e.getMessage();

        }
    }
    @RequestMapping(value = "/deleteBook",produces = "application/json")
    public String deleteBook(Integer bookId){
        log.info("逻辑意义上删除图书,bookId:"+bookId);
//        log.info("逻辑意义上删除图书,bookId:",bookId);
        try{
            BookInfo bookInfo=new BookInfo();
            bookInfo.setId(bookId);
            bookInfo.setStatus(BookStatusEnum.DELETED.getCode());
            bookservice.updateBook(bookInfo);
            return "逻辑意义上删除图书成功";
        }
        catch (Exception e){
            log.error("逻辑意义上删除图书失败");
            return e.getMessage();
        }

    }
    @RequestMapping(value = "/batchDelete",produces = "application/json")
    public String batchDelete(@RequestParam List<Integer> ids){
        log.info("开始批量删除,ids:{}",ids);
        try{
            bookservice.batchDelete(ids);
            return "批量删除成功";

        }catch (Exception e){
            log.info(e.getMessage());
            return "批量删除失败";
        }
    }














//    @RequestMapping("/getList")
//    public List<BookInfo> getList(){
////        Bookservice bookservice=new Bookservice();
//        List<BookInfo> bookInfos=bookservice.getList();
//        return bookInfos;
//    }
}
